//
//  CorgiHomeCell.swift
//  weibo
//
//  Created by Corgi on 17/3/24.
//  Copyright © 2017年 cocoaHangTou. All rights reserved.
//

import UIKit
import SnapKit
//间距
let HOMECELLMARGIN: CGFloat = 10

class CorgiHomeCell: UITableViewCell {

    //记录底部视图约束
    var bottomViewTopConstraint: Constraint?
    
    var statusViewModel: CorgiStatusViewModel? {
    
        didSet {
        
            originalView.statusViewModel = statusViewModel
            
            bottomViewTopConstraint?.deactivate()
            
            //判断是否包含转发微博(retweeted_status)
            if let _ = statusViewModel?.homeModel?.retweeted_status {
            
                retweetView.statusViewModel = statusViewModel
                
                retweetView.isHidden = false
                
                bottomView.snp.makeConstraints({ (make) in
                    bottomViewTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
                })
                
            
            } else {
                
                retweetView.isHidden = true
            
                bottomView.snp.makeConstraints({ (make) in
                    bottomViewTopConstraint = make.top.equalTo(originalView.snp.bottom).constraint
                })
            
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
        
        backgroundColor = UIColor(white: 235 / 255, alpha: 1)
        
        contentView.addSubview(originalView)
        
        contentView.addSubview(bottomView)
        
        contentView.addSubview(retweetView)
        
        originalView.snp.makeConstraints { (make) in
            make.top.trailing.equalTo(8)
            make.leading.trailing.equalToSuperview()
        }
        
        retweetView.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(originalView.snp.bottom)
            //make.height.equalTo(50)
        }
        
        bottomView.snp.makeConstraints { (make) in
            bottomViewTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
            make.leading.bottom.trailing.equalToSuperview()
            make.height.equalTo(35)
        }
        
    }
    
    private lazy var originalView: CorgiStatueOriginalView = CorgiStatueOriginalView()
    
    private lazy var retweetView: CorgiStatusRetweetView = CorgiStatusRetweetView()
    
    private lazy var bottomView: CorgiStatusBottomView = CorgiStatusBottomView()
}
